//package com.yang.sso.oauth.oauth.mode.mobile;
//
//
//import com.yang.sso.oauth.model.MyUserDetails;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AbstractAuthenticationToken;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.oauth2.provider.*;
//import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
//import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
//
//import java.util.LinkedHashMap;
//import java.util.Map;
//import java.util.Optional;
//
///**
// * 验证码授权
// *
// * @author: Islands
// * @create: 2024-04-13 12:49
// */
//
//public class SmsCodeTokenGranter extends AbstractTokenGranter {
//
//    @Autowired
//    private RbacFeignClient rbacFeignClient;
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//    private static final String SMS_GRANT_TYPE = GrentTypeEnum.SMS_GRANT_TYPE.getValue();
//
//    public SmsCodeTokenGranter(AuthorizationServerTokenServices tokenServices,
//                               ClientDetailsService clientDetailsService,
//                               OAuth2RequestFactory requestFactory) {
//        super(tokenServices, clientDetailsService, requestFactory, SMS_GRANT_TYPE);
//    }
//
//
//    @Override
//    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
//        Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
//        String phone = parameters.get("phone");
//        if(StringUtils.isBlank(phone)){
//            throw new ArithmeticException("手机号不能为空");
//        }
//        //客户端传入验证码
//        String smsCode = Optional.ofNullable(parameters.get("code"))
//                .orElse("0");
//        //缓存获取验证码
//        String cacheCode = Optional
//                .ofNullable(stringRedisTemplate.opsForValue().get("code:sms")).orElse(null);
//        if (!smsCode.equals(cacheCode)) {
//            //登录失败
//            throw new ArithmeticException("验证码已过期,请重新申请");
//        }
//        MyUserDetails userDetails = new MyUserDetails(rbacFeignClient.getAuthorizationUser(phone).getData());
//        Authentication userAuth =
//                new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//        ((AbstractAuthenticationToken) userAuth).setDetails(parameters);
//        OAuth2Request storedOAuth2Request =
//                getRequestFactory().createOAuth2Request(client, tokenRequest);
//        return new OAuth2Authentication(storedOAuth2Request, userAuth);
//    }
//}
